home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18023 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.6 KB  |  137 lines

  1. Path: news.sprintlink.net!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: INITIALIZATION of a MEMBER CLASS of a CLASS
  5. Date: Thu, 18 Apr 1996 11:51:21 -0400
  6. Organization: Datalytics, Inc
  7. Message-ID: <317664F9.1E16@datalytics.com>
  8. References: <4l37o3$iug@risky.ecs.umass.edu>
  9. NNTP-Posting-Host: 204.62.224.71
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Kiran K Toutireddy wrote:
  16. > Hi,
  17. >         I have a question related to initializing the member
  18. > classes inside a class data structure. The following example 
  19. > should explain the question.
  20. > I have a class called Engine, which can be initialized to Gas
  21. > engine,Deisel Engine, Electric Engine, etc. by default, it is
  22. > electric engine.(default constructo
  23. > I have a totally unrelated class(no inheritances...) called
  24. > Car.
  25. >         Now this Car class has a member
  26. >                 Engine car_engine;
  27. >         Since all the cars I consider have only gas engines(
  28. > no EVs considered :-),
  29. >         I want to initialize the car_engine member to be Gas
  30. > engine by default, instead of the default electric engine. How
  31. > do I do that?
  32. >         the following is some pseudo code of the above example
  33. >         class Engine{
  34. >                 nuts..bolts..pipes..rods...;
  35. >         public:
  36. >                 Engine(){  make this an electric engine,....;}
  37. >                 Engine(int Gas){ make it a gas engine..}
  38.  
  39. Since you can't have negative cylinders, wouldn't size_t be a 
  40. better type than int?
  41.  
  42. >                 Engine(float Deisel){ make it a deisel engine;}
  43.  
  44. float seems an even worse type for specifying the number of 
  45. cylinders.
  46.  
  47. >         };
  48.  
  49. How about this instead?
  50.  
  51.     class Engine
  52.     {
  53.         public:
  54.             enum engine_type
  55.             {
  56.                 ELECTRIC,
  57.                 GASOLINE,
  58.                 DIESEL
  59.             };
  60.  
  61.             Engine(
  62.                 engine_type i_Type = ELECTRIC,
  63.                 size_t i_Cylinders = 0);
  64.         private:
  65.             engine_type    m_Type;
  66.             size_t        m_Cylinders;
  67.             etc.
  68.     };
  69.  
  70. You could overload the ctor to avoid specifying cylinders for an 
  71. electric engine; maybe you want to specify the battery voltage?
  72.  
  73. >         class Car{
  74. >                 Engine car_engine;
  75. >         public:
  76. >                 Car(){ how do i initialize the car_engine to be of type Gas here?;}
  77. >         };
  78. > I tried initializing the car_engine by saying Engine
  79. > car_engine(int Gas), but it gives a warning saying that ANSI
  80. > c++ forbids such declarations.
  81. > Can anybody suggest a better way of doing this?
  82. > thanks in advance
  83. > -kiran
  84.  
  85. How about this:
  86.  
  87. class Car
  88. {
  89.     public:
  90.         Car(
  91.             const size_t i_Cylinders):
  92.             engine(i_Cylinders)
  93.         {
  94.         };
  95.     private:
  96.         Engine engine;
  97. };
  98.  
  99. I've assumed that you would want to specify the number of 
  100. cylinders in Car's ctor.
  101.  
  102. The solution is to initialize engine in the initializer list.
  103. (If you change Engine as I suggested above, the initializer 
  104. would be this:
  105.  
  106.             engine(
  107.                 Engine::GASOLINE,
  108.                 i_Cylinders)
  109. )
  110.  
  111. Of course, Engine could provide another mf for changing the 
  112. engine type after construction (which would be necessary if you 
  113. wanted an array of gasoline engines).  In that case, you could 
  114. leave the default initialization of engine and call the other mf 
  115. to change it to gasoline.
  116.  
  117. The latter approach is not as useful (except for arrays) because 
  118. of the work involved in invoking the default ctor then changing 
  119. the engine type.  The more a class does in the default ctor and 
  120. undoes in the other mf, the more useless work you cause your 
  121. code to perform.
  122.  
  123. -- 
  124. Robert Stewart        | My opinions are usually my own.
  125. Datalytics, Inc.    | stew@datalytics.com
  126.